package com.zrrd.yunchmall.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.order.entity.OrderReturnApply;
import com.zrrd.yunchmall.order.entity.OrderReturnReason;
import com.zrrd.yunchmall.order.service.IOrderReturnReasonService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 退货原因表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/returnReason")
public class OrderReturnReasonController {
    @Autowired
    private IOrderReturnReasonService orderReturnReasonService;

    @GetMapping("/list")
    @ApiOperation("退货原因列表")
    private ResponseResult list(@RequestParam int pageNum,@RequestParam int pageSize){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        return new ResponseResult<>(200,"查询成功",
                orderReturnReasonService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    @GetMapping("/{id}")
    @ApiOperation("查看退货详情")
    private ResponseResult select(@PathVariable("id") Integer id){
        OrderReturnReason orderReturnReason=orderReturnReasonService.getById(id);
        return new ResponseResult<>(200,"查询成功!",orderReturnReason);
    }
    @PostMapping("/update/{id}")
    @ApiOperation("修改退货原因")
    private ResponseResult update(@PathVariable("id") Integer id,
                                  @RequestBody OrderReturnReason orderReturnReason){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        return new ResponseResult<>(200,"修改成功!",orderReturnReasonService.update(orderReturnReason,updateWrapper));
    }
    @PostMapping("/create")
    @ApiOperation("添加退货原因")
    private ResponseResult create(@RequestBody OrderReturnReason orderReturnReason){

        return new ResponseResult<>(200,"添加成功!",orderReturnReasonService.save(orderReturnReason));
    }
    @PostMapping("/delete")
    @ApiOperation("添加退货原因")
    private ResponseResult delete(@RequestParam String ids){
        for (String id : ids.split(",")) {
            orderReturnReasonService.removeById(id);
        }
        return new ResponseResult<>(200,"删除成功!");
    }
    @PostMapping("/update/status")
    @ApiOperation("是否启用退货原因")
    private ResponseResult update(@RequestParam String ids,
                                  @RequestParam Integer status){
        for (String id : ids.split(",")) {
            UpdateWrapper updateWrapper=new UpdateWrapper<>();
            updateWrapper.set("status",status);
            updateWrapper.in("id",id);
            orderReturnReasonService.update(updateWrapper);
        }
        return new ResponseResult<>(200,"修改成功!");
    }
}
